<?php
class Scorll extends CWidget{
	public $id;
	public $title;
	protected $assetUrl;
	protected $cs;
	public $dirName = 'push';
	public $fileName = '';
	public $breakId = 3;
	public $extClass = '';
	public $express = 0;
	public $apiCfg = array();
	
	public function init(){
		$this->cs = Yii::app()->clientScript;
		$this->assetUrl = Yii::app()->assetManager->publish(dirname(__FILE__).'/asset');
		$this->regFiles();
		parent::init();
	}

	public function run(){
		$listarr = $this->getList();
		echo '<div class="goods_header end_header"><span class="morepush"><div class="navi"></div></span><h3>'.$this->title.'</h3></div>
		<div class="scollwp">
			<div class="scrollable" id="'.$this->id.'">
			   <div class="items">
					<div class="itemswp">';
		foreach($listarr as $k => $l){
			if($k!=0 && $k % $this->breakId == 0)
				echo '</div><div class="itemswp">';
			$title = strip_tags($l['title']);
			echo '<div class="good_wp">
					<div class="good_info">
						<div class="good_img"><a href="'.$l['click_url'].'" target="_blank"><img src="'.$l['pic_url'].'" alt="'.$title.'" /></a></div>
						<p class="good_title"><a href="'.$l['click_url'].'" target="_blank" title="'.$title.'">'.mb_substr($title,0,30,'utf-8').'</a></p>
						<p class="good_price"><span>￥</span>'.$l['price'].'<span> (30天售出 '.$l['volume'].' 件)</span></p>
						<p class="good_credit"><a class="seven_return" onfocus="this.blur();" href="javascript:void(0);" title="七天退换">七天退换</a><a class="real_desc" onfocus="this.blur();" href="javascript:void(0);" title="如实描述">如实描述</a><img src="'.Yii::app()->baseUrl.'/images/credit/'.$l['seller_credit_score'].'.gif" /></p>
					</div>
				</div>';
		}
		echo '</div>
			</div>
		</div></div>
		';

	}

	protected function getList(){
		$defaultCfg= array(
			'method' => 'taobao.taobaoke.items.get',
			'fields' => 'num_iid,title,pic_url,volume,price,shop_click_url,seller_credit_score,click_url',
			'page_size' => 15,
			'sort' => 'commissionVolume_desc',
			'start_credit' => '1crown',
			'sevendays_return' => '1',
			'real_describe' => '1',
			'cid' => 0,
			'keyword' => '',
			'outer_code'=>'miyifun',
		);

		$config = array_merge($defaultCfg,$this->apiCfg);
		//print_r($config);
		$api = new TaobaoApi($config);
		$result = $api->getData($this->express, $this->dirName, $this->fileName);
		return $result['taobaoke_items_get_response']['taobaoke_items']['taobaoke_item'];
	}

	protected function regFiles(){
		$this->cs->registerScriptFile($this->assetUrl.'/js/jquery.tools.min.js');
		$this->cs->registerCssFile($this->assetUrl.'/css/scrollable-horizontal.css');
		$this->cs->registerCssFile($this->assetUrl.'/css/scrollable-navigator.css');
		$this->cs->registerScript($this->id,"
			\$('#".$this->id."').scrollable({circular: true}).navigator().autoscroll({
				interval: 3000
			});
		");
	}
}